package com.taptech.doufu.ui.activity.me

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.taptech.doufu.R
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity
import com.taptech.doufu.ui.adapter.BaseRecyclerAdappter
import com.taptech.doufu.ui.adapter.VoucherAdapter
import com.taptech.doufu.util.GsonUtil
import com.taptech.doufu.weex.http.TFHttpUtil
import kotlinx.android.synthetic.main.activity_voucher.*

class VoucherActivity : DiaobaoBaseActivity() {

    var last = 0
    lateinit var adapter: VoucherAdapter
    var isFromVip = false
    var amount = 0
    var optionId = 0
    var ticketId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_voucher)
        isFromVip = intent.getBooleanExtra(INTENT_KEY_IS_FROM_VIP,false)
        amount = intent.getIntExtra(INTENT_KEY_AMOUNT,0)
        optionId = intent.getIntExtra(INTENT_KEY_OPTION_ID,0)
        ticketId = intent.getIntExtra(INTENT_KEY_TICKET_ID,0)
        layNotSelect.setOnClickListener {
            ivNotSelect.setBackgroundResource(R.drawable.icon_voucher_st)
            val intent = Intent()
            intent.putExtra(INTENT_KEY_TICKET_ID,0)
            intent.putExtra(INTENT_KEY_AMOUNT,0)
            setResult(Activity.RESULT_OK,intent)
            finish()
        }
        if (ticketId == 0) {
            ivNotSelect.setBackgroundResource(R.drawable.icon_voucher_st)
        }
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        adapter = VoucherAdapter(this)
        adapter.setOnItemClickListener(object :BaseRecyclerAdappter.OnItemClickListener<VoucherBean>{
            override fun onClick(item: VoucherBean, position: Int) {
                adapter.dataList.forEach {
                    it.select = false
                }
                item.select = true
                adapter.notifyDataSetChanged()
                val intent = Intent()
                intent.putExtra(INTENT_KEY_TICKET_ID,item.id)
                intent.putExtra(INTENT_KEY_AMOUNT,item.amount)
                setResult(Activity.RESULT_OK,intent)
                finish()
            }
        })
        mRecyclerView.adapter = adapter
        getData(isFromVip,amount,optionId)
    }


    fun getData(isFromVip:Boolean,amount:Int,optionId:Int) {
        val url = "http://api.doufu.la/getMyCashTickets"
        val params = mutableMapOf<String,Any>()
        params["size"] = 1000
        if (isFromVip) {
            params["amount"] = amount
            params["type"] = "VIP"
        }else{
            params["option_id"] = optionId
        }
        TFHttpUtil.requestGet(url,params){json, error ->
            try {
                val data = json.getJSONObject("data")
                last = data.getIntValue("last")
                val list = GsonUtil.jsonToList(data.getJSONArray("list").toJSONString(),Array<VoucherBean>::class.java)
                updateUI(list)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    fun updateUI(list: MutableList<VoucherBean>) {
        list.forEach {
            if (it.id == ticketId) {
                it.select = true
            }
        }
        adapter.setData(list)
    }


    data class VoucherBean(
            val id:Int = 0,
            val user_id:Int = 0,
            val ticket_id:Int = 0,
            val amount:Float = 0f,
            val min_amount:Float = 0f,
            val expire_date:String = "",
            val status:Int = 0,
            val best:Boolean = false,
            var select:Boolean = false
    )

    companion object{
        const val INTENT_KEY_IS_FROM_VIP = "intent_key_is_from_vip"
        const val INTENT_KEY_AMOUNT = "intent_key_amount"
        const val INTENT_KEY_OPTION_ID = "intent_key_option_id"
        const val INTENT_KEY_TICKET_ID = "intent_key_ticket_id"
        fun startActivity(context: Context,isFromVip:Boolean = false,amount:Int = 0,optionId:Int = 0,ticketId:Int = 0) {
            val intent = Intent()
            intent.setClass(context,VoucherActivity::class.java)
            intent.putExtra(INTENT_KEY_IS_FROM_VIP,isFromVip)
            intent.putExtra(INTENT_KEY_TICKET_ID,ticketId)
            intent.putExtra(INTENT_KEY_AMOUNT,amount)
            intent.putExtra(INTENT_KEY_OPTION_ID,optionId)
            context.startActivity(intent)
        }

        fun startActivityForResult(activity: Activity,isFromVip:Boolean = false,amount:Int = 0,optionId:Int = 0,ticketId:Int = 0) {
            val intent = Intent()
            intent.setClass(activity,VoucherActivity::class.java)
            intent.putExtra(INTENT_KEY_IS_FROM_VIP,isFromVip)
            intent.putExtra(INTENT_KEY_TICKET_ID,ticketId)
            intent.putExtra(INTENT_KEY_AMOUNT,amount)
            intent.putExtra(INTENT_KEY_OPTION_ID,optionId)
            activity.startActivityForResult(intent, 0)
        }
    }
}